//
//  HBMoneyTextField.m
//  HBLibary
//
//  Created by Rambo on 2018/12/19.
//

#import "HBMoneyTextField.h"
#import "NSString+HBCategory.h"

@interface HBMoneyTextField () <UITextFieldDelegate>

@end

@implementation HBMoneyTextField

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        self.maxMoney = 999999.99;
        self.keyboardType = UIKeyboardTypeDecimalPad;
        self.delegate = self;
    }
    return self;
}

- (void)didMoveToSuperview{
    [super didMoveToSuperview];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChange) name:UITextFieldTextDidChangeNotification object:nil];
}

- (void)removeFromSuperview{
    [super removeFromSuperview];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender{
    return NO;
}

- (void)textChange{
    if ([self.text isEqualToString:@"0"]) {
        self.text = @"";
    }
}


- (void)setDelegate:(id<UITextFieldDelegate>)delegate{
    if (delegate != self) {
        delegate = self;
    }
    [super setDelegate:delegate];
}

- (void)setMaxMoney:(CGFloat)maxMoney{
    NSString *money = [NSString stringWithFormat:@"%.2f",maxMoney];
    _maxMoney = money.floatValue;
}

- (void)setKeyboardType:(UIKeyboardType)keyboardType{
    [super setKeyboardType:UIKeyboardTypeDecimalPad];
}

#pragma mark - UITextFieldDelegate


- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
//    NSLog(@"%@",string);
    if ([NSString hb_validateNumber:string] ) {
        
        if ([string isEqualToString:@"0"] && [textField.text isEqualToString:@"0.0"]) {
            textField.text = @"0.01";
            return NO;
        }
        
        NSRange dotRange = [textField.text rangeOfString:@"."];
        
        if (!textField.hasText && [string isEqualToString:@"0"]) {
            textField.text = @"0.";
            return NO;
        }
        
        
        else if (dotRange.location != NSNotFound) {
            if (range.location < dotRange.location+3) {
                return YES;
            }
            
            else{
                return NO;
            }
        }
        else if ([textField.text containsString:@"."]) {
            return YES;
        }
        
        else if ([textField.text stringByAppendingString:string].floatValue > _maxMoney){
            return NO;
        }
        return YES;
        
    }
    
    else if ([string isEqualToString:@"."]){
//        NSLog(@"%@------%zd",textField.text,textField.text.integerValue);
        if (!textField.hasText) {
            textField.text = @"0.";
            return NO;
        }else if ([textField.text stringByAppendingString:string].floatValue > _maxMoney ){
            
            return NO;
        }
        else if (textField.text.floatValue >= _maxMoney){
            return NO;
        }
        else if([textField.text containsString:@"."]){
            return NO;
        }else{
            return YES;
        }
        
    }
    
    else if ([string isEqualToString:@"\n"] || [string isEqualToString:@""]){
//        NSLog(@"%@",textField.text);
        if ([textField.text isEqualToString:@"0."]) {
            textField.text = @"";
            return NO;
        }
        return YES;
    }
    return NO;
}


@end
